<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    var page = require('webpage').create();
    var system = require('system');

    ////读取命令行参数，也就是js文件路径。
    if (system.args.length === 1) {
        console.log('Usage: loadspeed.js <some URL>');
//这行代码很重要。凡是结束必须调用。否则phantomjs不会停止
        phantom.exit();
    }
    page.settings.loadImages = true;  //加载图片
    page.settings.resourceTimeout = 30000;//超过10秒放弃加载
    //截图设置，
    //page.viewportSize = {
    //width: 210,
    //height: 297
    //};



    //uuid
    //特别鸣谢作者:原文链接：https://blog.csdn.net/weixin_40612082/article/details/81085892

    var address = system.args[1];
    page.open(address, function(status) {
        function getUUID() {
            var d = new Date().getTime();
            if (window.performance && typeof window.performance.now === "function") {
                d += performance.now(); //use high-precision timer if available
            }
            var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                var r = (d + Math.random() * 16) % 16 | 0;
                d = Math.floor(d / 16);
                return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
            });
            return uuid;
        }

        function checkReadyState() {//等待加载完成将页面生成pdf
            setTimeout(function () {
                var readyState = page.evaluate(function () {
                    return document.readyState;
                });

                if ("complete" === readyState) {

                    page.paperSize = { width:'595px',height:'842px',orientation: 'portrait',border: '1cm' };
                    var timestamp = Date.parse(new Date());
                    var pdfname = 'HT_'+timestamp + Math.floor(Math.random()*1000000);
                    //只需要改这一处，输出路径
                    // var outpathstr = "C:\\Users\\LSK\\Downloads\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\"+pdfname+".pdf";
                    //本地路径
                    var outpathstr="C:\\Users\\LSK\\IdeaProjects\\driver\\out\\artifacts\\driver_war_exploded\\pdf\\"+getUUID().toString+".pdf";
                    page.render(outpathstr);
                    //page.render("c://test.png");
                    //console.log就是传输回去的内容。
                    console.log("生成成功");
                    console.log("$"+outpathstr+"$");
                    phantom.exit();

                } else {
                    checkReadyState();
                }
            },1000);
        }
        checkReadyState();
    });
</script>
</html>